#include "kernel/reaction.hpp"

Reaction:: ~Reaction() throw()
{
}

Reaction:: Reaction( const string & _name, const Library &lib, lua_State *L, const string & _K ) :
name( _name ),
K( L, _K, false),
library( lib ),
items_()
{
	
}

Reaction:: Reaction( const Reaction &r ) :
name( r.name ),
K( r.K ),
library( r.library ),
items_( r.items_ )
{
	
}

const string & Reaction:: key() const throw() { return name; } 

Reaction:: Item:: ~Item() throw()
{
}

Reaction:: Item:: Item( const string & _id, const int _nu ) :
id( _id ),
nu( _nu )
{
}

Reaction:: Item:: Item( const Item &item ) :
id( item.id ),
nu( item.nu )
{
}

void Reaction:: append( const int nu, const string &id )
{
	if( ! library.has( id ) )
		throw exception("*** Reaction<%s>: unknonw '%s'", name.c_str(), id.c_str());
	if( 0 == nu )
		throw exception("*** Reaction<%s>: '%s' as nu=0", name.c_str(), id.c_str());
	
	for( size_t i= items_.size(); i>0; --i )
	{
		if( items_[i].id == id )
			throw exception("*** Reaction<%s>: already got '%s'", name.c_str(), id.c_str()); 
	}
	const Item item(id,nu);
	items_.push_back( item );
}

const array<Reaction::Item> & Reaction:: items() const throw() { return items_; }

Real Reaction:: getK( Real t ) const
{
	Lua::Function<Real> &F = (Lua::Function<Real> &)K;
	return F(t);
}
